package com.pignest.hotel.ui.income;

import java.util.ArrayList;
import java.util.List;

import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Button;
import org.zkoss.zul.Checkbox;
import org.zkoss.zul.Label;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Radiogroup;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

import com.pignest.framework.util.Utils;
import com.pignest.hotel.beans.Living;
import com.pignest.hotel.beans.TbAccounts;
import com.pignest.hotel.definition.InComeItemType;
import com.pignest.hotel.definition.LivingState;
import com.pignest.hotel.service.IncomeService;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.system.InComeUtil;
import com.pignest.hotel.util.DateUtil;
import com.pignest.hotel.util.MoneyUtil;
import com.pignest.hotel.util.UIUtils;

public class InComeManageWindow extends Window implements EventListener {

	/**
	 * 账务处理
	 * 入账、结账、账目打印
	 */
	private static final long serialVersionUID = 5153099134057868938L;

	private  IncomeService incomeService;
	private List<Living> selectLive;
	private Label storage;
	private Label used;
	private Label balance;
	private Label payed;//已付金额针对部分结账的
	private Listbox livingsListbox;
	private Listbox accoutsListbox;
	private Listbox aListbox;
	private Button inComeBtn;
	private Radiogroup sGroup;
	private Checkbox aUnCheckChb;
	private Button bCheckOutBtn;//部分结账
	
	private Textbox rRoomName;
	private Textbox rCustormName;
	private Textbox rRemark;
	
	@Override
	public void onEvent(Event event) throws Exception {
		// TODO Auto-generated method stub
		if ("onCreate".equals(event.getName())) {
			storage =(Label)getFellow("storage");
			used =(Label)getFellow("used");
			balance =(Label)getFellow("balance");
			payed=(Label)getFellow("payed");
			livingsListbox=(Listbox)getFellow("livingsListbox");
			accoutsListbox=(Listbox)getFellow("accoutsListbox");
			aListbox=(Listbox)getFellow("aListbox");
			inComeBtn=(Button)getFellow("inComeBtn");
			sGroup =(Radiogroup)getFellow("sGroup");
			aUnCheckChb=(Checkbox)getFellow("aUnCheckChb");
			bCheckOutBtn=(Button)getFellow("bCheckOutBtn");
			rRoomName=(Textbox)getFellow("rRoomName");
			rCustormName=(Textbox)getFellow("rCustormName");
			rRemark=(Textbox)getFellow("rRemark");
			init();
			sGroup.addEventListener(Events.ON_CHECK, new EventListener(){
				   public void onEvent(Event event) {
					   queryStatic();
				   }
			   });
			aUnCheckChb.addEventListener(Events.ON_CHECK, new EventListener(){
				   public void onEvent(Event event) {
						  queryAccount(selectLive);
						  allStatic(selectLive);
						  bCheckOutBtn.setDisabled(true);
				   }
			   });
		
		}
	}
	public InComeManageWindow()
	{
		incomeService =Services.getIncomeService();
		addEventListener("onCreate", this);
	}
	public void init(){
		if(selectLive==null)
			return ;
		allStatic(selectLive);
		queryLiving();
		queryAccount(selectLive);
		queryStatic();
	}

	public void queryLiving()
	{
		livingsListbox.getItems().clear();
		int index=1;
		for(final Living l:selectLive)
		{
			 Listitem listitem = new Listitem();
			 listitem.setValue(l);
			 listitem.setParent(livingsListbox);
			 listitem.appendChild(new Listcell(String.valueOf(index)));
			 listitem.appendChild(new Listcell(l.getRoom().getName()));
			 listitem.appendChild(new Listcell(l.getCustormName()));
			 listitem.appendChild(new Listcell(MoneyUtil.toString(l.getAdjustPrice())));
			 listitem.appendChild(new Listcell(DateUtil.toString(l.getLiveTime(), DateUtil.HOTEL_DEFAULT_FORMAT)));
			 listitem.appendChild(new Listcell(LivingState.valueOf(l.getLiveState()).getDisplayName()));
			 listitem.addEventListener(Events.ON_CLICK, new EventListener(){
				   public void onEvent(Event event) {
					   if(aUnCheckChb.isChecked())
					   {
						   queryAccount(selectLive); 
						   allStatic(selectLive);
					   }else{
						   List<Living> livings=new ArrayList<Living>();
						   livings.add(l);
						   queryAccount(livings);
						   allStatic(livings);
						   bCheckOutBtn.setDisabled(false);
					   }
					   initLivingRemark(l);
				   }
			   });
			 index++;
		}
	}
	/**查询消费项目
	 * */
	public void queryAccount(List<Living> livings)
	{
		accoutsListbox.getItems().clear();
		List<TbAccounts> acc=incomeService.queryAccoutsByMulLiving(livings);
		int aIndex =1;
		if(acc==null)
			return;
		for(TbAccounts ac:acc){
			Listitem listitem = new Listitem();
			 listitem.setValue(ac);
			 listitem.setParent(accoutsListbox);
			 listitem.appendChild(new Listcell(String.valueOf(aIndex)));
			 listitem.appendChild(new Listcell(ac.getName()));
			 listitem.appendChild(new Listcell(InComeItemType.valueOf(ac.getType()).getName()));
			 listitem.appendChild(new Listcell(MoneyUtil.toString(ac.getPrice())));
			 listitem.appendChild(new Listcell(MoneyUtil.toString(ac.getPrice2())));
			 listitem.appendChild(new Listcell(String.valueOf(ac.getCounts())));
			 listitem.appendChild(new Listcell(ac.getEndstate()));
			 listitem.appendChild(new Listcell(ac.getLiving().getRoom().getName()));
			 listitem.appendChild(new Listcell(DateUtil.toString(ac.getOptime(), DateUtil.HOTEL_DEFAULT_FORMAT)));
			 if(ac.getPrice()>=0){
				 listitem.setCheckable(false);
				 listitem.setSelected(false);
			 }
			 aIndex++;
		}
	}
	public void queryStatic()
	{
		String s =sGroup.getSelectedItem().getValue();
		aListbox.getItems().clear();
		List sl =null;
		if(s.equals("true"))
		    sl =incomeService.queryAccStaticByMulLiving(selectLive, true);
		else
			sl=incomeService.queryAccStaticByMulLiving(selectLive, false);
		if(sl==null)
			return;
		for(int i=0;i<sl.size();i++)
		{
			Object []o =(Object[])sl.get(i);
			String value =(String)o[1];
			String name ="";
			if(s.equals("false"))
			   name =InComeItemType.valueOf(value).getName();
			else
				name=value;
			Long d=(Long)o[0];
			Listitem listitem = new Listitem();
			listitem.setParent(aListbox);
			listitem.appendChild(new Listcell(MoneyUtil.toString(d)));
			listitem.appendChild(new Listcell(name));
		}
	}
	public void allStatic(List<Living> livings)
	{
		InComeUtil.countInComeNoAdd(livings, storage, used, payed, balance);
	}
	public void initLivingRemark(Living l)
	{
		rRoomName.setValue(l.getRoom().getName());
		rCustormName.setValue(l.getCustormName());
		rRemark.setValue(l.getRemark());
	}
	/**
	 * 部分结账
	 * 过程：
	 * 1如果部分结账的总额超过已存入的金额则不结
	 * 
	 * **/
	public void reRoad()
	{
		selectLive=incomeService.queryBySelf(selectLive);
		init();
	}
	public List<Living> getSelectLive() {
		return selectLive;
	}
	public void setSelectLive(List<Living> selectLive) {
		this.selectLive = selectLive;
	}

}
